# [GSW-048] System Properties
MY_DIR := vendor/google/custom/prop

define build-custom-dir
$(eval INSTALLED_CUSTOM_DIR := $(1)/custom)

ALL_DEFAULT_INSTALLED_MODULES += $(INSTALLED_CUSTOM_DIR)/lost
ALL_DEFAULT_INSTALLED_MODULES += $(INSTALLED_CUSTOM_DIR)/device
ALL_DEFAULT_INSTALLED_MODULES += $(INSTALLED_CUSTOM_DIR)/sku
ALL_DEFAULT_INSTALLED_MODULES += $(TARGET_OUT)/app/factoryapp

$(INSTALLED_CUSTOM_DIR)/lost:
	@echo "build-custom-dir: $(INSTALLED_CUSTOM_DIR)/lost"
	$(hide) mkdir -p $(INSTALLED_CUSTOM_DIR)/lost

$(INSTALLED_CUSTOM_DIR)/device:
	@echo "build-custom-dir: $(INSTALLED_CUSTOM_DIR)/device"
	$(hide) mkdir -p $(INSTALLED_CUSTOM_DIR)/device

$(INSTALLED_CUSTOM_DIR)/sku:
	@echo "build-custom-dir: $(INSTALLED_CUSTOM_DIR)/sku"
	$(hide) mkdir -p $(INSTALLED_CUSTOM_DIR)/sku
$(TARGET_OUT)/app/factoryapp:
	@echo "build-factoryapp-dir: $(TARGET_OUT)/app/factoryapp"
	$(hide) mkdir -p $(TARGET_OUT)/app/factoryapp
endef

define build-prop-file
$(eval _partition := $(1))
$(eval _target_device := $(2))
$(eval _skuid := $(3))
$(eval _product_brand := $(4))
$(eval _target_product := $(5))
$(eval _primary_product := $(6))
$(eval _product_model := $(7))
$(eval _product_nickname := $(8))
$(eval _build_number := $(9))
$(eval _base_os_ver := $(10))
$(eval _prop_file_name := $(11))

$(eval _build_fingerprint := $(_product_brand)/$(_target_product)/$(_target_device):$(PLATFORM_VERSION)/$(BUILD_ID)/$(_build_number):$(TARGET_BUILD_VARIANT)/$(BUILD_VERSION_TAGS))
$(eval _primary_build_fingerprint := $(_product_brand)/$(_primary_product)/$(_target_device):$(PLATFORM_VERSION)/$(BUILD_ID)/$(_build_number):$(TARGET_BUILD_VARIANT)/$(BUILD_VERSION_TAGS))
$(eval _property_file_path := $(_partition)/custom/$(_target_device)/$(_skuid)/etc/$(_prop_file_name))
$(eval _base_property_file := $(MY_DIR)/$(_target_device)/$(_skuid)/$(_target_product).prop)
$(eval _base_os := )
$(if $(_base_os_ver),
$(eval _base_os := $(_product_brand)/$(_target_product)/$(_target_device):$(PLATFORM_VERSION)/$(BUILD_ID)/$(_base_os_ver):$(TARGET_BUILD_VARIANT)/$(BUILD_VERSION_TAGS))
)

ALL_DEFAULT_INSTALLED_MODULES += $(_property_file_path)
$(_property_file_path):
	$(hide) echo build-prop-file: $(_property_file_path)
	$(hide) mkdir -p $(dir $(_property_file_path))
	$(hide) cat $(_base_property_file) > $(_property_file_path)
	$(hide) echo "ro.product.model=$(_product_model)" >> $(_property_file_path)
	$(hide) echo "ro.product.nickname=$(_product_nickname)" >> $(_property_file_path)
	$(hide) echo "ro.build.fingerprint=$(_build_fingerprint)" >> $(_property_file_path)
	$(hide) echo "ro.build.version.base_os=$(_base_os)" >> $(_property_file_path)

	$(hide) $(foreach partition, bootimage system system_ext system_dlkm product,
		$(hide) echo "ro.$(partition).build.fingerprint=$(_primary_build_fingerprint)" >> $(_property_file_path)
		$(hide) echo "ro.product.$(partition).device=$(_target_device)" >> $(_property_file_path)
		$(hide) echo "ro.product.$(partition).model=$(_product_model)" >> $(_property_file_path)
		$(hide) echo "ro.product.$(partition).name=$(_target_product)" >> $(_property_file_path)
	)
endef


define build-tablet-prop-file
$(eval _partition := $(1))
$(eval _product := $(2))
$(eval _product_device := $(3))
$(eval _skuid := $(4))
$(eval _product_brand := $(5))
$(eval _target_product := $(6))
$(eval _primary_product := $(7))
$(eval _product_model := $(8))
$(eval _product_nickname := $(9))
$(eval _build_number := $(10))
$(eval _base_os_ver := $(11))
$(eval _prop_file_name := $(12))

$(eval _build_fingerprint := $(_product_brand)/$(_target_product)/$(_product_device):$(PLATFORM_VERSION)/$(BUILD_ID)/$(_build_number):$(TARGET_BUILD_VARIANT)/$(BUILD_VERSION_TAGS))
$(eval _primary_build_fingerprint := $(_product_brand)/$(_primary_product)/$(_product_device):$(PLATFORM_VERSION)/$(BUILD_ID)/$(_build_number):$(TARGET_BUILD_VARIANT)/$(BUILD_VERSION_TAGS))
$(eval _property_file_path := $(_partition)/custom/$(_product)/$(_skuid)/etc/$(_prop_file_name))
$(eval _base_property_file := $(MY_DIR)/$(_product)/$(_skuid)/$(_product_device)-$(_target_product).prop)
$(eval _base_os := )
$(if $(_base_os_ver),
$(eval _base_os := $(_product_brand)/$(_target_product)/$(_product_device):$(PLATFORM_VERSION)/$(BUILD_ID)/$(_base_os_ver):$(TARGET_BUILD_VARIANT)/$(BUILD_VERSION_TAGS))
)

ALL_DEFAULT_INSTALLED_MODULES += $(_property_file_path)
$(_property_file_path):
	$(hide) echo build-prop-file: $(_property_file_path)
	$(hide) mkdir -p $(dir $(_property_file_path))
	$(hide) cat $(_base_property_file) > $(_property_file_path)
	$(hide) echo "ro.product.model=$(_product_model)" >> $(_property_file_path)
	$(hide) echo "ro.product.nickname=$(_product_nickname)" >> $(_property_file_path)
	$(hide) echo "ro.build.fingerprint=$(_build_fingerprint)" >> $(_property_file_path)
	$(hide) echo "ro.build.version.base_os=$(_base_os)" >> $(_property_file_path)

	$(hide) $(foreach partition, bootimage system system_ext system_dlkm product,
		$(hide) echo "ro.$(partition).build.fingerprint=$(_primary_build_fingerprint)" >> $(_property_file_path)
		$(hide) echo "ro.product.$(partition).device=$(_product_device)" >> $(_property_file_path)
		$(hide) echo "ro.product.$(partition).model=$(_product_model)" >> $(_property_file_path)
		$(hide) echo "ro.product.$(partition).name=$(_target_product)" >> $(_property_file_path)
	)
endef

# $(1): Target product
# $(2): Partition name
define build-property-files


ifneq (,$(findstring Storm, $(1)))
	$(call build-prop-file, $(2), STR, 100WW, PIXL,0AWW,0AWW, PIXL VIBE2, PIXL VIBE2, $(PIXL_BUILD_NUMBER), $(BASE_OS_VERSION), 0-nfc-build.prop)
	$(call build-prop-file, $(2), STR, 100WW, PIXL,00WW,0AWW, PIXL VIBE2, PIXL VIBE2, $(PIXL_BUILD_NUMBER), $(BASE_OS_VERSION), 1-nfc-build.prop)
	$(call build-prop-file, $(2), STR, 100ZA, PIXL,0AZA,0AWW, PIXL VIBE2, PIXL VIBE2, $(PIXL_BUILD_NUMBER), $(BASE_OS_VERSION), 0-nfc-build.prop)
	$(call build-prop-file, $(2), STR, 100EEA, PIXL,00EEA,0AWW, PIXL VIBE2, PIXL VIBE2, $(PIXL_BUILD_NUMBER), $(BASE_OS_VERSION), 1-nfc-build.prop)
	$(call build-prop-file, $(2), STR, 1HMWW, PIXL,HMAWW,0AWW, PIXL VIBE2, PIXL VIBE2, $(PIXL_BUILD_NUMBER), $(BASE_OS_VERSION), 0-nfc-build.prop)
endif

endef


$(eval $(call build-property-files,$(TARGET_PRODUCT),$(TARGET_OUT_PRODUCT)))

$(eval $(call build-property-files,$(TARGET_PRODUCT),$(TARGET_RECOVERY_ROOT_OUT)/product))

$(eval $(call build-custom-dir,$(TARGET_OUT_PRODUCT)))
